package com.digitalbonestudios.slugpug.tv;

import java.io.IOException;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.util.Log;

import com.digitalbonestudios.framework.gl.Animation;
import com.digitalbonestudios.framework.gl.SpriteBatcher;
import com.digitalbonestudios.framework.gl.TextureRegion;
import com.digitalbonestudios.framework.impl.GLGraphics;

public class WorldRenderer {
	static final float FRUSTUM_WIDTH = 12;//15;
	static final float FRUSTUM_HEIGHT = 7;//10;
	GLGraphics glGraphics;
	World world;
	SpriteBatcher batcher;
	TexFont font;

	public WorldRenderer(Context context, GLGraphics glGraphics, SpriteBatcher batcher, World world) {
		this.glGraphics = glGraphics;
		this.world = world;
		this.batcher = batcher;
		
		GL10 gl = glGraphics.getGL();
		font = new TexFont(context, gl);
        
        try {
			font.LoadFont("Baveuse.bff", gl);
			Log.v("PugFont", "Font loaded successfully!");
		} catch (IOException e) {
			Log.v("PugFont", "The font did not load successfully!");
			e.printStackTrace();
		}
	}

	public void render() {
		Dog dog = world.dog;
        BodyPart head = dog.parts.get(0);
        DogTreat treat = world.treat;
        
		GL10 gl = glGraphics.getGL();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		/////START SPRITE BATCHING///////////
		batcher.beginBatch(Assets.pugItems);
		/////////////////////////////////////
		
			//Draw background
			//batcher.drawSprite(400, 240, 800, 480, Assets.background);
			batcher.drawSprite(384, 224, 768, 448, Assets.background);
		
			//Draw random dog treat
			TextureRegion item = null;
	        switch (treat.type) {
	        case DogTreat.BACON:
	        	item = Assets.bacon;
	        	break;
	        case DogTreat.BISCUIT:
	        	item = Assets.biscuit;
	        	break;
	        case DogTreat.BONE:
	        	item = Assets.bone;
	        	break;
	        case DogTreat.STEAK:
	        	item = Assets.steak;
	        	break;
	        default:
	        	item = Assets.bacon;
	        }
	        
	        int x = treat.x * 64;
	        int y = treat.y * 64; 
	        //Adding 32 to the x and y values to make sure they are drawing on screen
	        batcher.drawSprite(x+32, y+32, 64, 64, item);
	        
	        //Draw dog poop
	        TextureRegion keyFrame = Assets.steamingPoop.getKeyFrame(world.poop.stateTime, Animation.ANIMATION_LOOPING);
	        batcher.drawSprite(world.poop.x * 64 + 32, world.poop.y * 64 + 32, 64, 64, keyFrame);
	        
	        //Draw dog body
	        int len = dog.parts.size();
	        for(int i = 1; i < len - 1; i++) {
	            BodyPart part = dog.parts.get(i);
	            x = part.x * 64;
	            y = part.y * 64;
	            batcher.drawSprite(x+32, y+32, 64, 64, Assets.body);
	        }
	        
	        //Create Tail
	        BodyPart part = dog.parts.get(len - 1);
	        int tailX = part.x * 64 + 32;
	        int tailY = part.y * 64 + 32;
	        
	        //Setup Head
	        x = head.x * 64 + 32;
	        y = head.y * 64 + 32;
	        
	        TextureRegion headPixmap = null;
	        TextureRegion tailPixmap = Assets.body;//Assets.tailUp;
	        if(dog.direction == Dog.UP) {
	            headPixmap = Assets.headDown;
	        }
	        if(dog.direction == Dog.LEFT) {
	            headPixmap = Assets.headLeft;
	        }
	        if(dog.direction == Dog.DOWN) {
	            headPixmap = Assets.headUp;
	        }
	        if(dog.direction == Dog.RIGHT) {
	            headPixmap = Assets.headRight;
	        }
	        
	        BodyPart endPiece = dog.parts.get(dog.parts.size()-2);
	        BodyPart tail = dog.parts.get(dog.parts.size()-1);
	        
	        int tX = tail.x - endPiece.x;
	        int tY = tail.y - endPiece.y;
	        
	        if (tX == 0 && tY == 1)
	        	tailPixmap = Assets.tailDown;
	        if (tX == 0 && tY == -1)
	        	tailPixmap = Assets.tailUp;
	        if (tX == 1 && tY == 0)
	        	tailPixmap = Assets.tailLeft;
	        if (tX == -1 && tY == 0)
	        	tailPixmap = Assets.tailRight;
	        
	        // For one frame after eating a treat the
	        // position of the tail and end piece are 
	        // identical, so we will base the calculations 
	        // off the tail and second to last piece
	        if (tX == 0 && tY == 0) {
	        	BodyPart midPiece = dog.parts.get(dog.parts.size()-3);
	        	int mX = tail.x - midPiece.x;
	        	int mY = tail.y - midPiece.y;
	        	if (mX == 0 && mY == 1)
		        	tailPixmap = Assets.tailDown;
		        if (mX == 0 && mY == -1)
		        	tailPixmap = Assets.tailUp;
		        if (mX == 1 && mY == 0)
		        	tailPixmap = Assets.tailLeft;
		        if (mX == -1 && mY == 0)
		        	tailPixmap = Assets.tailRight;
	        }
	        
	        
	        //Draw Head
	        batcher.drawSprite(x, y, 64, 64, headPixmap);
	        
	        //Draw Tail        
	        batcher.drawSprite(tailX, tailY, 64, 64, tailPixmap);
			
		/////END SPRITE BATCHING/////////////
		batcher.endBatch();
		/////////////////////////////////////
		
		//font.PrintAt(gl, "SCORE: 0", 64, 32);
	}

}
